SERVER_WEB_NAME=niu-video/niu-video-backend

CID=$(docker ps -a | grep "${SERVER_WEB_NAME}" | awk '{print $1}')
IID=$(docker images | grep "${SERVER_WEB_NAME}" | awk '{print $3}')

if [ -n "${CID}" ]; then
  echo "存在容器${SERVER_WEB_NAME},CID=${CID}"
  docker stop ${CID}
  echo "成功停止容器${SERVER_WEB_NAME},CID=${CID}"
  docker rm ${CID}
  echo "成功删除容器${SERVER_WEB_NAME},CID=${CID}"
fi

if [ -n "${IID}" ]; then
  echo "存在镜像${SERVER_WEB_NAME},IID=${IID}"
  docker rmi ${IID}
  echo "成功删除镜像${SERVER_WEB_NAME},IID=${IID}"
fi
echo "开始构建镜像${SERVER_WEB_NAME}"

docker build -t ${SERVER_WEB_NAME} -f ../deploy/Dockerfile ..
echo "成功构建镜像${SERVER_WEB_NAME}"
docker run --name niu-video-backend --restart=always -p 8099:8099 -v /logs/niu-video/niu-video-backend:/niu-video-backend/logs -d -it --privileged=true -it ${SERVER_WEB_NAME}
echo "成功创建并运行容器${SERVER_WEB_NAME}"